package com.ustb.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ustb.springboot.common.utils.R;
import com.ustb.springboot.common.utils.TokenUtil;
import com.ustb.springboot.entity.AnswerBlankEntity;
import com.ustb.springboot.entity.FormEntity;
import com.ustb.springboot.entity.QuestionBlankEntity;
import com.ustb.springboot.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;


/**
 * 表单属
 *
 * @author pyn
 * @email 1661829453@qq.com
 * @date 2022-05-02 20:58:29
 */
@RestController
@RequestMapping("form")
public class FormController {
    @Autowired
    private FormService formService;
    @Autowired
    private AnswerRadioService answerRadioService;
    @Autowired
    private AnswerBlankService answerBlankService;
    @Autowired
    AnswerCheckboxService answerCheckboxService;
    @Autowired
    AnswerDatetimeService answerDatetimeService;
    @Autowired
    AnswerListboxService answerListboxService;
    @Autowired
    AnswerScoreService answerScoreService;
    @Autowired
    AnswerSlideService answerSlideService;
    @Autowired
    UserService userService;
    /**
     * 根据用户查询表单列表 返回表单（form）
     * 参数为RecycleBin
     */
    @RequestMapping("/queryFormList")
    @RequiresPermissions("form:queryFormList")
    public R queryFormList(HttpServletRequest request, @RequestBody Map<String, Object> body) {
        return formService.queryFormList(request, body);
    }

    /**
     *
     * 保存表单
     */
    @RequestMapping("/saveForm")
    @RequiresPermissions("form:saveForm")
    public R saveForm(HttpServletRequest request, @RequestBody Map<String, Object> body) {
        return formService.saveForm(request, body);
    }


    /**
     *
     * 根据表单ID删除表单
     **/
    @RequestMapping("/deleteForm")
    @RequiresPermissions("form:deleteFrom")
    public R deleteForm(@RequestBody Map<String, Object> body) {
        return formService.deleteFormById(body);
    }


    /**
     * ToDo
     * 提交表单
     */
    @RequestMapping("/submitForm")
    @RequiresPermissions("form:submitForm")
    public R submitForm(HttpServletRequest request, @RequestBody Map<String, Object> body) {
        return formService.submitForm(request, body);
    }

    /**
     * 查询原始表单（不返回作答信息）
     * 根据表单id 查询表单信息和问题、选项等信息
     */
    @RequestMapping("/showForm")
    @RequiresPermissions("form:showForm")
    public R showForm(HttpServletRequest request, @RequestBody Map<String, Object> body) {
        return formService.showForm(request, body);
    }

    /**
     * 修改表单状态
     */
    @RequestMapping("/editFormStatus")
    @RequiresPermissions("form:editFormStatus")
    public R editFormStatus(@RequestBody Map<String, Object> body) {
        return formService.editFormStatus(body);
    }

    /**
     *  ToDo
     * 查询作答表单
     * 根据表单id 查询表单信息和问题、选项等信息
     */
    @RequestMapping("/checkResult")
    @RequiresPermissions("form:checkResult")
    public R checkResult(@RequestBody Map<String, Object> body) {
        return formService.checkResult(body);
    }
    //TODO:统计问卷填写结果，做图形化展示
    /**
     * 返回表单所有答案
     * 都是 answer
     0 text文本题
     1 radio单选题
     2 checkbox多选题
     3 listbox下拉
     4 slide滑动条
     5 datetime日期时间
     * 1，先根据form_id找question
     * 2，根据question_id找问题的答案
     * */
    @RequestMapping("/FormToTalResult")
    @RequiresPermissions("form:FormToTalResult")
    public R FormResult(HttpServletRequest request, @RequestBody Map<String, Object> body) {
        Map<String,Object> TotalAnswer = new HashMap<String , Object>();
        TotalAnswer = formService.QueryFormResult(request, body);
        if (null == TotalAnswer)
            return R.ok(2000, "暂无信息!");
        else
            return R.ok(20000, "查询成功!").put("form", TotalAnswer);
    }

    @RequestMapping("/queryWithId")
    @RequiresPermissions("form:queryWithId")
    public R queryWithId(@RequestBody Map<String,Object> body){
        List<FormEntity> form =new ArrayList<>();
        form = formService.querywithId(body.get("id").toString());
        return  R.ok(20000, "查询成功!").put("form", form);
    }
    @RequestMapping("/updateForm")
    @RequiresPermissions("form:updateForm")
    public R update(@RequestBody FormEntity formEntity) {
        return formService.updateById(formEntity) ? R.ok(20000, "更新成功") : R.error(2000, "更新失败");
    }
    @RequestMapping("savework")
    @RequiresPermissions("form:savework")
    public R saveWork(HttpServletRequest request, @RequestBody Map<String, Object> body) {
        return formService.saveWork(request,body);
    }
    @RequestMapping("/summerize")
    @RequiresPermissions("form:summerize")
    public R summerize(@RequestBody Map<String,Object> body){
        Map<String,Object> summerizeMessages = new HashMap<String , Object>();
        Integer type = Integer.parseInt(body.get("question_type").toString());
        switch (type) {
            case 1:
                summerizeMessages = answerRadioService.summerizeRadioMessage(body);
                break;
            case 2:
                summerizeMessages = answerCheckboxService.summerizeCheckboxMessage(body);
                break;
            case 3:
                summerizeMessages = answerListboxService.summerizeListBoxMessages(body);
                break;
            case 4:
                summerizeMessages = answerSlideService.summerizeSlide(body);
                break;
            case 0:
                List<AnswerBlankEntity> ans =  answerBlankService.queryAnsBlankWithQuestionId(body.get("question_id").toString());
                summerizeMessages = answerBlankService.summerizeAnswerBlankTable(ans);
                break;
            case 5:
                summerizeMessages = answerDatetimeService.summerizeDatetimeMessage(body);
                break;
            case 6:
                summerizeMessages = answerScoreService.summerize(body);
            default:
                break;
        }
        if (null == summerizeMessages)
            return R.ok(2000, "暂无信息!");
        else
            return R.ok(20000, "查询成功!").put("summerize",summerizeMessages);
    }
    @RequestMapping("querywithdepartmentid")
    @RequiresPermissions("form:querywithdepartmentid")
    public R queryWithDepartmentId(HttpServletRequest request, @RequestBody Map<String, Object> body){
       //Integer userId = TokenUtil.getId(request);
        List<Object> departmentForm = new ArrayList<>();
        List<FormEntity> formEntityList = formService.queryWithdepartmentId(body);
        for(FormEntity formEntity : formEntityList){
            Map<String,String> Form = new HashMap<>();
            Form.put("departmentId",formEntity.getDepartmentId());
            Form.put("userName",userService.queryUserNameWithUserId(formEntity.getUserId().toString()));
            Form.put("nickName",userService.queryNickNameWithUserId(formEntity.getUserId().toString()));
            Form.put("FormInfo",formEntity.getFormInfo());
            Form.put("formUrl",formEntity.getFormUrl());
            Form.put("Id",formEntity.getId());
            Form.put("FormPassword",formEntity.getFormPassword());
            Form.put("beginTime",formEntity.getBeginTime().toString());
            Form.put("endTime",formEntity.getEndTime().toString());
            Form.put("createTime",formEntity.getCreateTime().toString());
            Form.put("formCounts",formEntity.getCounts().toString());
            Form.put("formStatus",formEntity.getFormStatus().toString());
            Form.put("ipLimit",formEntity.getIpLimit().toString());
            Form.put("isAnonymous",formEntity.getIsAnonymous().toString());
            Form.put("timeLimit",formEntity.getTimeLimit().toString());
        }
        if (null == departmentForm)
            return R.ok(2000, "暂无信息!");
        else
            return R.ok(20000, "查询成功!").put("departmentForm",departmentForm);
    }

}
